# Copyright (C) 2007 Denis Crowdy, Macquarie University, Australia
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of voluntary contributions made by many
# individuals. For exact contribution history, see the revision
# history and logs.
#
# Author: Denis Crowdy <Denis.Crowdy@mq.edu.au>

import os
import os.path
import shutil
import logging
import subprocess
import sys
from string import split
from socket import *

from pylons import config
from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect
from arkaiv.model.oggzinfo import OggzInfo
from arkaiv.model.cmmlparser import cmmlParser
from arkaiv.model.framer import Frame
from authkit.authorize.pylons_adaptors import authorize, authorized
from authkit.permissions import HasAuthKitRole, RemoteUser, ValidAuthKitUser, UserIn

from arkaiv.controllers import *
from arkaiv.lib.base import *
#from arkaiv.lib.base import BaseController, render

import arkaiv.model as model

log = logging.getLogger(__name__)

archive_loc = config['archive_loc']
path_base = config['media_path_base']

myhostname = gethostname()
url_base = "http://" + myhostname + ":5000/"
media_url_base = config['media_url_base']

class PageController(BaseController):

    def displayadd(self):
        # Get the list of collections
        collectionlist = model.getcollections()
        # Wrap the names in <option> tags for the h.select webhelper
        c.collectioninfo = ""
        for collection in collectionlist:
            c.collectioninfo = c.collectioninfo + "<option>" + collection['name'] + "</option>"

        return render('/addform.mak')



    def search(self):
        searchterms = request.params['searchterms']

        # actually we can only search for a single search term at the moment To
        # search for more terms, we should split the searchterms strings here
        # and send the model a list of search terms it can search through.  If,
        # at this stage, we assume that the process is "and", that should be
        # reasonably straightforward.
        if authorized(HasAuthKitRole('admin')):
            role = "admin"
        else:
            role = "public"

        c.clips = model.searchforclips(searchterms,role)
        c.items = model.searchforitems(searchterms,role)
        c.metas = model.searchformetas(searchterms,role)

        c.playlists=model.getplaylists()
        #c.playlistinfo = ""
        c.playlistinfo = [] 
        for playlist in c.playlists:
#            c.playlistinfo = c.playlistinfo + "<option>" + playlist['name'] + "</option>"
            c.playlistinfo.append(playlist['name'])
 
        return render('/searchresults.mak')


#class PageController(BaseController):
#
    def index(self):
#        # Return a rendered template
        return render('/page.mako')
#        # or, return a response
#        return 'Hello World'

